<?php
namespace app\api\controller;
use think\facade\Session;
use think\facade\Cache;
use Firebase\JWT\JWT;
use app\common\model\user\User as UserModel;
use app\common\model\power\Rolepowerlnk;
use app\common\model\power\Rolepower;
use app\common\model\company\Company;
use app\common\model\cost\Costjournal;
use app\common\model\orderhawa\HawaList;
use app\common\model\orderdelivery\DeliveryList;
use app\common\model\orderreceipt\ReceiptList;
use app\common\model\transit\Transitcompany;
class User extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
	}

	/**
	 * 账号完善/注册
	 * @return [type] [description]
	 */
	public function register(){
		$iphone = input('phoneno');
		$code = input('pwd');
		$name = input('username');
		$sex = input('sex');
		$user = UserModel::getinfo();
		if($sex!=='1'&&$sex!=='2'){
			return myerr('参数错误');
		}
		if(empty($user->Iphone)){
			if(empty($iphone)){
				return myerr('请输入手机号');
			}
			if(empty($name)){
				return myerr('请输入您的真实姓名');
			}
			if(empty($code)){
				return myerr('请输入验证码');
			}
			if(CAPTCHASTATUS){
				$sb = Cache::get('s_iphone_'.$iphone,'');
				if(empty($sb)){
					return myerr('验证码验证失败，请重新获取验证码');
				}
				if($sb !== $code){
					return myerr('验证码错误，请重新输入');
				}
			}
			//判断是否需要绑定账号
			$ipuser = UserModel::getinfo($iphone);
			if(!empty($ipuser)){
				$change = false;
				if(empty($ipuser->WechatId)&&!empty($user->WechatId)){
					$change = true;
					$ipuser->WechatId = $user->WechatId;
				}
				if(empty($ipuser->MpWechatId)&&!empty($user->MpWechatId)){
					$change = true;
					$ipuser->MpWechatId = $user->MpWechatId;
				}
				if($change==false){
					return myerr('绑定失败，此手机号已绑定其他微信');
				}
				$ipuser->save();
				$user->delete();
				return myok('账号合并成功，请重新登录','','relogin');
			}
			//完善账号
			$user->Iphone = $iphone;
			$user->Name = $name;
			$user->Sex = $sex;
		}else{
			if(!empty($iphone)){
				if(empty($code)){
					return myerr('请输入验证码');
				}
				if(CAPTCHASTATUS){
					$sb = Cache::get('s_iphone_'.$iphone,'');
					if(empty($sb)){
						return myerr('验证码验证失败，请重新获取验证码');
					}
					if($sb !== $code){
						return myerr('验证码错误，请重新输入');
					}
				}
				$user->Iphone = $iphone;
			}
			if(!empty($name)){
				$user->Name = $name;
			}
			if(!empty($sex)){
				$user->Sex = $sex;
			}
		}
		$user->save();
		return myok('绑定成功');
	}
	/**
	 * 获取用户注册/修改时的信息
	 * @return [type] [description]
	 */
	public function getregisterinfo(){
		$userlogininfo = Pub::getuserid();
        if(!$userlogininfo['code']){
            return myerr($userlogininfo['msg']);
        }
        $data = UserModel::getinfo();
        if(empty($data['Iphone'])){
			$rdata = [];
		}else{
			$rdata['username'] = $data->Name;
        	$rdata['iphone']  = $data->Iphone;
        	$rdata['sex']  = $data->Sex;
		}
		return myok($rdata);		
	}
	 /**
     * 返回用户名称和手机号
     * @return [type] [description]
     */
    public function apigetinfo(){
        $userlogininfo = Pub::getuserid();
        if(!$userlogininfo['code']){
            return myerr($userlogininfo['msg']);
        }
        $data = UserModel::getinfo();
        if(empty($data['Iphone'])){
			return myerr('请完善您的信息','','register');
		}

        $rdata['username'] = $data->Name;
        $rdata['iphone']  = $data->Iphone;
        $rdata['sex']  = $data->Sex;
        $rdata['role']  = $data->Role;
        if($data->Role==101){
        	$company  =  Company::user_getinfo();
        	$rdata['companyname'] = empty($company)?'暂无绑定公司':$company->Name;
        }else if($data->Role==106){
        	$company  =  Transitcompany::user_getinfo();
        	$rdata['companyname'] = empty($company)?'暂无绑定公司':$company->Name;
        }else{
        	$rdata['companyname'] = COMPANYNAME;
        }
        $rdata['rolename'] = getrolename($data->Role);
        $rdata['power'] = Rolepowerlnk::getpowerlnk('user');
        if(Rolepower::ispower(203008)){
        	$sb =Costjournal::getsummary();
        	$timestr = '今天';
        	$urlstr = '&starttime='.date('Y-m-d').'&endtime='.date('Y-m-d');
        	if(empty($sb)){
        		$sb = Costjournal::getsummary(date('Y-m',time()).'-01',date('Y-m-d',time()));
        		$urlstr = '&starttime='.date('Y-m',time()).'-01'.'&endtime='.date('Y-m-d',time());
        		$timestr = '本月';
        	}
        	if(!empty($sb)){
        		$rdata['summary'][] = [round($sb['PayCost']),$sb['PayCostUnit'],$timestr.'支出','public/list?type=costpay'.$urlstr];
        		$rdata['summary'][] = [round($sb['RevenueCost']),$sb['RevenueCostUnit'],$timestr.'收入','public/list?type=costrevenue'.$urlstr];
        	}
        }

        if(Rolepower::ispower(204002)){
        	$sb = HawaList::getsummary();
        	$timestr = '今天';
        	$urlstr = '&starttime='.date('Y-m-d').'&endtime='.date('Y-m-d');
        	if(empty($sb)){
        		$timestr = '本月';
        		$sb = HawaList::getsummary(date('Y-m',time()).'-01',date('Y-m-d H:i',time()));
        		$urlstr = '&starttime='.date('Y-m',time()).'-01'.'&endtime='.date('Y-m-d',time());
        	}
        	if(!empty($sb)){
        		if($data->Role==203){
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type=orderhawaacco'.$urlstr];
        		}else{
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'回收','public/list?type=manaorderhawa'.$urlstr];
        		}
        	}
        	$sb = ReceiptList::getsummary();
        	$timestr = '今天';
        	$urlstr = '&starttime='.date('Y-m-d').'&endtime='.date('Y-m-d');
        	if(empty($sb)){
        		$timestr = '本月';
        		$sb = ReceiptList::getsummary(date('Y-m',time()).'-01',date('Y-m-d H:i',time()));
        		$urlstr = '&starttime='.date('Y-m',time()).'-01'.'&endtime='.date('Y-m-d',time());
        	}
        	if(!empty($sb)){
        		if($data->Role==203){
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type=orderreceiptacco'.$urlstr];
        		}else{
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'入库','public/list?type=manaorderreceipt'.$urlstr];
        		}
        	}
        	$sb = DeliveryList::getsummary();
        	$timestr = '今天';
        	$urlstr = '&starttime='.date('Y-m-d').'&endtime='.date('Y-m-d');
        	if(empty($sb)){
        		$timestr = '本月';
        		$sb = DeliveryList::getsummary(date('Y-m',time()).'-01',date('Y-m-d H:i',time()));
        		$urlstr = '&starttime='.date('Y-m',time()).'-01'.'&endtime='.date('Y-m-d',time());
        	}
        	if(!empty($sb)){
        		if($data->Role==203){
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type=orderdeliveryacco'.$urlstr];
        		}else{
        			$rdata['summary'][] = [$sb['Weight'],$sb['WeightUnit'],$timestr.'出库','public/list?type=manaorderdelivery'.$urlstr];
        		}
        		
        	}
        	// $sb = ::getsummary();
        	// $sb = Hawalist::getsummary();
        }
        if(!empty($rdata['summary'])){
        	foreach ($rdata['summary'] as $key => $value) {
        		$rdata['summary'][$key][0] = round($rdata['summary'][$key][0],2);
        	}
        }
       	if($data->Role==101 && empty($company)){
       	 	return myok($rdata,'','gohawacreate');
       	}if($data->Role==106 && empty($company)){
       	 	return myok($rdata,'','gotransitcompanycreate');
       	}else{
       		return myok($rdata);	
       	}
    } 

	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}

	}
}
